package org.eclipse.soc.ide4edu.assignment.editors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.FormDialog;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

public class EditorDialog extends FormDialog {

	StyledText edit;
	BufferStore buffStore;
	EditorContent content;

	final String TextMarker = "--TEXT--";
	final String StyleMarker = "--STYLE--";

	public EditorDialog(Shell shell, BufferStore buf) {
		super(shell);
		buffStore = buf;
	}

	@Override
	protected Control createButtonBar(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		composite.setLayout(layout);

		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		Button save = new Button(composite, SWT.PUSH);
		save.setText("Save");
		save.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleSave();
			}

		});
		save.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		Button done = new Button(composite, SWT.PUSH);
		done.setText("Done");
		done.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleSave();
				close();
			}

		});
		save.setLayoutData(gd);

		gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		Button cancel = new Button(composite, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				close();
			}

		});
		save.setLayoutData(gd);

		return composite;
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = managedForm.getForm();
		FormToolkit toolkit = managedForm.getToolkit();
		GridLayout layout = new GridLayout();
		form.getBody().setLayout(layout);

		Composite composite = toolkit.createComposite(form.getBody());
		GridData gd = new GridData(GridData.FILL_BOTH);
		composite.setLayout(layout);
		composite.setLayoutData(gd);

		content = new EditorContent(composite, edit);
		content.createEditorContent();

		edit = content.getTextinstance();

		if (buffStore.getBuffer() != null) {
			buffStore.initEdit(edit, getShell().getDisplay());
		}

	}

	private String getStylesInfo() {
		String data = null;
		StyleRange[] styles = edit.getStyleRanges();
		data = new Integer(styles.length).toString();
		for (int i = 0; i < styles.length; i++) {
			data += " " + styles[i].start + " " + styles[i].length + " "
					+ styles[i].fontStyle + " " + styles[i].underline + " "
					+ styles[i].strikeout;

			if (styles[i].foreground != null) {
				data += " " + styles[i].foreground.getRed() + " "
						+ styles[i].foreground.getGreen() + " "
						+ styles[i].foreground.getBlue();
			} else
				data += " " + "0 0 0";

			if (styles[i].background != null)
				data += " " + "0 255 255";
			else
				data += " " + "255 255 255";
		}

		return data;
	}

	private void handleSave() {
		try {

			final String buffer = TextMarker + "\n" + edit.getText() + "\n"
					+ TextMarker + "\n" + StyleMarker + "\n" + getStylesInfo()
					+ "\n" + StyleMarker;

			buffStore.setBuffer(buffer);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
